package com.jumper.mydemo.statics;

import java.util.Iterator;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.jumper.mydemo.my.MyConfiguration;
import com.jumper.mydemo.my.MyController;

public class ConfigurableListableBeanFactoryDemo {
    public static void run() throws NoSuchFieldException, SecurityException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfiguration.class);
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        beanFactory.ignoreDependencyType(String.class);
        beanFactory.ignoreDependencyInterface(BeanFactory.class);
        beanFactory.registerResolvableDependency(ApplicationContext.class, applicationContext);

        String beanName = "myService";
        DependencyDescriptor dependencyDescriptor = new DependencyDescriptor(MyController.class.getDeclaredField("myService"), false);
        boolean isAutowireCandidate = beanFactory.isAutowireCandidate(beanName, dependencyDescriptor);
        System.out.println(beanName + "是否为自动注入候选者:" + isAutowireCandidate);

        BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
        System.out.println(beanName + "的BeanDefinition:" + beanDefinition);

        Iterator<String> beanNamIterator = beanFactory.getBeanNamesIterator();
        System.out.println("所有Bean的名称:");
        beanNamIterator.forEachRemaining(System.out::println);

        beanFactory.clearMetadataCache();
        beanFactory.freezeConfiguration();

        boolean isConfigurationFrozen = beanFactory.isConfigurationFrozen();
        System.out.println("配置是否已冻结:" + isConfigurationFrozen);

        beanFactory.preInstantiateSingletons();
    }
}
